<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>

<html>
<head>
	<title>公司信息管理</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
	
	</style>
	
	<script src="${ctxStatic}/jquery-plugin/jquery.blockUI.js" type="text/javascript"></script>
	<script type="text/javascript">
		var trades = "";
		var returnValue = true;
		//下拉列表    选择省份    查询相应省份的  所有 城市
		function findArea(elem) {
			
			var provinceId = $(elem).val();
			$("#city option:not(:first)").remove();
			$("#city").val("-1");
			if (provinceId == "-1") {
				$("#s2id_city").find("a:first").find("span:first").html("请选择城市");
				return false;
			}
	
			$.ajax({
				type : "post",
				data : "parent_id=" + provinceId,
				url : "${ctx}/crm/business/customer/findCity",
				success : function(data) {
					for ( var i = 0; i < data.length; i++) {
						var nodes = "<option value='"+data[i].id+"'>"
								+ data[i].name + "</option>";
						$("#city").append(nodes);
					}
				}
			});
			$("#s2id_city").find("a:first").find("span:first").html("请选择城市");
		}
		function clearMessage(){
			$("#message").remove();
			$("#pMessage").append("<div id='message' style='padding-left:50%;color:red'></div>");
		}
	/* 	$("#namePreview li a:first").onpropertychange(function(){
			alert("aaa");
			clearMessage();
		}); */
		//======================================================================
		$(function(){
			 
			//清除行业信息
			$("#testTrade option:first").attr("selected","selected");
			$("#tradeView").text("");
			
			
			//选择行业
			$("#testTrade").change(function(){
				var trade = $(this).val();
				if(trades.indexOf(trade)>=0||trade=="-1"){
					$(this).val("-1");
					$("#s2id_testTrade").find("a span:first").text("请选择");
					return false;
				}
				trades = trades + trade + ",";
				
				var tradeName = $("option[value='"+trade+"']").text();
				var tradeView = $("#tradeView").text();
				if(tradeView == ""){
					$("#tradeView").text(tradeName);
				}else{
					$("#tradeView").text(tradeView+","+tradeName);
				}
				$(this).val("-1");
				$("#s2id_testTrade").find("a span:first").text("请选择");
			});
		});
		//检查上传文件类型
		function checkUploadFile(){
			 var fileName = "";
			 fileName = $("#namePreview li a:first").text(); 

			 if(fileName != ""){

				 var type =/\.[^\.]+/.exec(fileName);
				
				 if(type==".xls"||type==".xlsx"){ 
			       	  
			        return true;
			     }  
					 
			 }else{
				 
				 return false;
			 }
				 
		     
		    
		}
		
		function clerasd(){
			$("#tradeView").text("");
			trades="";
		}
		
	</script>
	
	<script type="text/javascript">
		$(document).ready(function() {
			//使用blockUI
			$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);
			/*  $("#blockbtn").click(function(){
				 $.blockUI({message:'正在处理，请等待...'});
			 }) */
			$("#btnSubmit").click( function () {
				
				
				clearMessage();
				if($("#tradeView").text()==""){
					$("#message").text("请选择行业!");
					return false;
				}
				
				if(!checkUploadFile()){
					$("#message").text("没有选择文件或文件格式错误!");
					return false;
				}
				var file = $("#name").val();
				trade = $("#tradeView").text();	
				contentType:"application/json;charset=UTF-8";
				var address = $("select option:selected :not(:first)").text();
				var province = $("select option:selected :eq(1)").val();
				var city = $("select option:selected :last").val();
			  	var url = "${ctx}/crm/dataImport/baseInformation/getAllByExcel";
				var args = {"trade":encodeURI(trade),"address":encodeURI(address),
						"file":file,"province":province,"city":city,"time":new Date()};
				$.getJSON(url,args,function(data){
					$("#successMessage").text("导入成功!");
					if(data.length>0){
						$("#duplicateData").text("如下公司已存在--请选择你要更新的公司");
						$("#duplicateData").append("</br></br>");
						$("#duplicateData").append("<input type='checkbox' name='checkAll' onclick='checkAlls(this)'/>全选/全不选");
						$("#duplicateData").append("</br></br>");
					}
					for(var i=0;i<data.length;i++){
						//alert(data[i].name);
						$("#duplicateData").
						append("<input type='checkbox' name='checkbox' value='"+i+"' />"+data[i].name+"</br></br>");
					}
					if(data.length>0){
						$("#duplicateData").append("<input type='submit' value='确认更新' class='btn btn-primary'/>");
					}
				});
			});
		});
	</script>
	<script type="text/javascript">
		function checkAlls(obj){
			$("input[name='checkbox']").prop('checked', $(obj).prop('checked'));
		}
	</script>
	
	<style type="text/css">
		.input-xlarge{
			width:350px;
		}
		.control-group{
			padding:5px;
			padding-top:0px;
			padding-left:50px;
		}
	</style>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active"><a href="javascript:void(0)">数据导入</a></li>
	</ul><br/>
	
	<form:form id="inputForm" modelAttribute="customer" action="" method="post" class="form-horizontal">
		
		
		
		
		<sys:message content="${message}"/>		
					
				<div class="control-group" id="controlGroup">
					<div class="controls" style="float:left;margin-left:11px" onchange="clearMessage()">
						<label class="control-label">行业选择：</label>
						<select  class="input-xlarge required" id="testTrade">
							<option value="-1">请选择</option>
							<c:forEach items="${dict}" var="dict">
								<option value="${dict.id}">${dict.label}</option>
							</c:forEach>
						</select>
					</div>
					
					<div style="float:left;margin-left:45px">
						<label>省份:</label>
						<select class="input-medium" id="province"  onchange="findArea(this)">
							<option  value="-1">请选择省份</option>
							<c:forEach items="${area}" var="area">
								<option  value="${area.id}">${area.name}</option>
							</c:forEach>
						</select>
					
						<label>城市:</label>
						<select class="input-medium" id="city">
							<option  value="-1">请选择城市</option>
						</select>
					</div>
					
					<div style="float:left;margin-left:45px">
						
						<form:hidden path="name" htmlEscape="false" maxlength="255" class="input-xlarge"/>	
						<sys:ckfinder input="name" type="files"  uploadPath="/test"/>
						
					<!-- 
						<label>选择文件:</label> 
						<a href="/../static/ckfinder/ckfinder.html">
							<input class="btn-primary" type="button"  value="上传文件" onchange="clearMessage()">
						</a> -->
						
						 
					</div>
				</div>
				<div class="control-group" id="controlGroup">
					<label class="control-label">公司行业:</label>
					<div class="controls">
						<p id="tradeView" style="float:left;"></p>&nbsp;&nbsp;&nbsp;
						<input type="button" onclick="clerasd()" value="清除"/>
					</div>
				</div>
				
				<p id="pMessage"><div id="message" style="padding-left:50%;color:red"></div></p>
		<div class="form-actions" style="padding-left:40%">
			<input id="btnSubmit" class="btn btn-primary" type="button" value="确认导入"/>
		</div>
	</form:form>
	<div id="successMessage" style="margin-left:40%;color:red;font-size:24px"></div>
	<form method="post" action="${ctx}/crm/dataImport/baseInformation/updateDuplicateData">
		<input type="checkbox" name="checkbox" value="-1" checked="checked" style="display:none"/>
		<div id="duplicateData" style="margin-left:39%;margin-top:20px;font-size:24px"></div>
	</form>
</body>
</html>